package com.taoxue.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.taoxue.dao.CountryDao;
import com.taoxue.model.Country;
@Component
public class CountryDaoImpl implements CountryDao {
	@Autowired
	private SessionFactory sessionFactory;
	private Log log = LogFactory.getLog(CountryDaoImpl.class);
	public void save(Country c) {
		log.debug("Start: CountryDaoImpl.save(" + c.toString() + ")");
		sessionFactory.getCurrentSession().saveOrUpdate(c);
		log.info("Done: CountryDaoImpl.save(" + c.toString() + ")");
	}
	public void delete(Country c) {
		log.debug("Start: CountryDaoImpl.delete(" + c.toString() + ")");
		sessionFactory.getCurrentSession().delete(c);
		log.info("Done: CountryDaoImpl.delete(" + c.toString() + ")");
	}
	@SuppressWarnings("unchecked")
	public List<Country> query(String hql) {
		List<Country> list = null;
		log.debug("Start: CountryDaoImpl.query(" + hql + ")");
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		list = query.list();
		if(log.isDebugEnabled()) {
			String result = null;
			for(Country c:list) {
				result += "[ " + c.toString() + " ]\n";
			}
			log.debug("Result: CountryDaoImpl.query\n" + result);
		}
		log.info("Done: CountryDaoImpl.query(" + hql + ")");
		return list;
	}
}
